package com.lianzai.camera.controller;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.camera.model.CameraBlackAndWhiteDO;
import com.lianzai.camera.service.CameraBlackAndWhiteService;
import com.lianzai.car.domain.CarInfoDO;
import com.lianzai.car.service.CarInfoService;
import com.lianzai.common.controller.BaseController;
import com.lianzai.common.utils.PageUtils;
import com.lianzai.common.utils.Query;
import com.lianzai.common.utils.R;
import com.lianzai.mobile.domain.CarPriceRelaDO;
import com.lianzai.mobile.domain.CarPriceRuleDO;
import com.lianzai.mobile.service.CarPriceRelaService;
import com.lianzai.mobile.service.CarPriceRuleService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/car/black")
public class CameraBlackAndWhiteController extends BaseController {
	@Autowired
	private CameraBlackAndWhiteService cameraBlackAndWhiteService;

	@GetMapping()
	String black(){
		return "car/black/black";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CameraBlackAndWhiteDO> carInfoList = cameraBlackAndWhiteService.list(query);
		int total = cameraBlackAndWhiteService.count(query);
		PageUtils pageUtils = new PageUtils(carInfoList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
		return "car/black/add";
	}

	@GetMapping("/choose")
	String choose(){
		return "car/black/community";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		/*CarPriceRuleDO carPriceRule = carPriceRuleService.get(id);
		List<Map> segList = carPriceRuleService.getSegInfo(id);
		model.addAttribute("carPriceRule", carPriceRule);
		model.addAttribute("segList", segList);*/
		return "car/black/edit";
	}

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(HttpServletRequest request, @RequestBody JSONObject obj){
        Map map = new HashMap();
        String carNum = obj.getString("carNum").toUpperCase();
        String type = obj.getString("type");
        String communityId = obj.getString("communityId");
        String startTime = obj.getString("startTime");
        String endTime = obj.getString("endTime");
        map.put("carNum",carNum);
        map.put("type",type);
        map.put("communityId",communityId);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        //查询下是否存在如果不存在则插入 如果存在则 更新
        Map existMap = cameraBlackAndWhiteService.queryExistBlackList(map);
        if(existMap == null){
            if(cameraBlackAndWhiteService.save(map)>0){
                return R.ok();
            }
        }else{
            if(cameraBlackAndWhiteService.update2(map)>0){
                return R.ok();
            }
        }

        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    public R remove( HttpServletRequest request, @RequestBody JSONObject obj){
        String carNum = obj.getString("carNum");
        Long communityId = obj.getLong("communityId");
        Integer type = obj.getInteger("type");
        if(cameraBlackAndWhiteService.remove(carNum,communityId,type)>0){
            return R.ok();
        }
        return R.error();
    }
}
